package cn.com.aiton.common.utils;

import cn.com.aiton.common.model.ServiceResult;
import cn.com.aiton.common.model.gbt209992007.GbtPlanDomain;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class JsonToPlanUtil {
    private static final Logger LOG =
            LoggerFactory.getLogger(JsonToPlanUtil.class);
    public static ServiceResult<List<GbtPlanDomain>> json2PlanList(String json){
        ServiceResult<List<GbtPlanDomain>> map = new ServiceResult<>();
        try {
            JSONArray array = new JSONArray(json);
            List<GbtPlanDomain> list = new ArrayList<>();
            for(int i=0;i<array.length();i++){
                JSONObject object = array.getJSONObject(i);
                GbtPlanDomain command = new GbtPlanDomain();
                command.setPlanNumber(object.getInt("id"));
                command.setScheduleNumber(object.getInt("scheduleId"));
                command.setWeek(listToInt(objectToList(object.getJSONArray("weekList"))));
                command.setDay(listToInt(objectToList(object.getJSONArray("dayList"))));
                command.setMonth(listToInt(objectToList(object.getJSONArray("monthList"))));
                list.add(command);
            }
            map.setSuccess(true);
            map.setObject(list);
            return map;
        }catch (Exception e){
            LOG.error(e.getMessage(),e);
            map.setSuccess(false);
            map.setMessage("数据转换出错");
            return map;
        }
    }

    public static int listToInt(List<Integer> list){
        int i = 0;
        for(int j:list){
            i = i | (1<<(j));
        }
        return i;
    }
    /**
     * 将List相位数组转换成Int32 位的数字
     * 32位中，低位0 备用 ，低位1开始为相位1 ，低位2为相位2 ........
     *
     * @param list
     * @return
     */
    public static int list2Int(List<Integer> list) {
        int i = 0;
        for (int j = 0; j < list.size(); j++) {
            if (list.get(j) != 0)
                i = i | (1 << (j));
        }
        return i;
    }
    /**
     * 将List相位数组转换成Int32 位的数字
     * 32位中，低位0 备用 ，低位1开始为相位1 ，低位2为相位2 ........
     *
     * @param list
     * @return
     */
    public static int list2intByMonth(List<Integer> list) {
        int i = 0;
        for (int j = 0; j < list.size(); j++) {
            if (list.get(j) != 0)
                i = i | (1 << (j));
        }
        return i;
    }
    public static List<Integer> objectToList(JSONArray array) throws JSONException {
        List<Integer> list = new ArrayList<>();
        for(int i=0;i<array.length();i++){
            list.add(array.getInt(i));
        }
        return list;
    }

    public static List<Integer> dayConvertToIntList(int i){

        List<Integer> list = new ArrayList<>();

        for (int j = 0; j < 31; j++){
            if(BinaryUtils.get(i, j)==1){
                list.add(j+1);
            }else{
                list.add(0);
            }
        }

        return list;
    }

    public static List<Integer> monthConvertToIntList(int i){

        List<Integer> list = new ArrayList<>();

        for(int j=0;j<12;j++){
            if((i & (0x1 << j)) >> j==1){
                list.add(j+1);
            }else{
                list.add(0);
            }
        }

        return list;
    }
    public static List<Integer> weekConvertToIntList(int i){

        List<Integer> list = new ArrayList<>();

        for(int j=0;j<7;j++){
            if((i & (0x1 << j)) >> j==1){
                list.add(j+1);
            }else{
                list.add(0);
            }
        }

        return list;
    }
}
